<?php
namespace App\Services;
use Exception,Log;
use GuzzleHttp\Client;

class CommonService
{
    public static function getRandomCode()
    {
        $dictionaries = [["A","B","a","b","0"],["C","D","c","d","1"],["E","F","e","f","2"],["G","H","g","h","3"],["I","J","i","j","4"],["K","L","k","l","5"],["M","N","m","n","6"],["O","P","o","p","7"],["Q","R","q","r","8"],["S","T","s","t","9"]];
        $times = str_split(time());
        $code = "";
        foreach ($times as $time) {
            $dictionarie = $dictionaries[$time];
            $code .= $dictionarie[array_rand($dictionarie,1)];
        }
        return $code;
    }

    public static function getRandomKfId()
    {
        $dictionaries = [["A","B","a","b","0"],["C","D","c","d","1"],["E","F","e","f","2"],["G","H","g","h","3"],["I","J","i","j","4"],["K","L","k","l","5"],["M","N","m","n","6"],["O","P","o","p","7"],["Q","R","q","r","8"],["S","T","s","t","9"]];
        $times = str_split(time().rand(100000,999999));
        $code = "";
        foreach ($times as $time) {
            $dictionarie = $dictionaries[$time];
            $code .= $dictionarie[array_rand($dictionarie,1)];
        }
        return $code;
    }

    public static function getRandomCodes($codes)
    {
        $dictionaries = [["A","B","a","b","0"],["C","D","c","d","1"],["E","F","e","f","2"],["G","H","g","h","3"],["I","J","i","j","4"],["K","L","k","l","5"],["M","N","m","n","6"],["O","P","o","p","7"],["Q","R","q","r","8"],["S","T","s","t","9"]];
        $times = str_split($codes);
        $code = "";
        foreach ($times as $time) {
            $dictionarie = $dictionaries[$time];
            $code .= $dictionarie[array_rand($dictionarie,1)];
        }
        return $code;
    }

    public static function getFixedCodes($codes)
    {
        $dictionaries = ["n","c","m","f","b","a","p","q","e","v"];
        $times = str_split($codes);
        $code = "";
        foreach ($times as $time) {
            $code .= $dictionaries[$time];
        }
        return $code;
    }

    public static function b64dec($b64) {
        // 64进制转换成10进制
        $dict = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=';
        $dec = 0;
        $len = strlen($b64);
        for ($i = 0; $i < $len; $i++) {
            $b = strpos($dict, $b64[$i]);
            $j = $len - $i - 1;
            $dec += ($j == 0 ? $b : (2 << (6 * $j - 1)) * $b);
        }
        return $dec;
    }

    public static function decb64($dec) {
        // 10进制转换成64进制，最长5位
        $dict = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=';
        $b64 = '';
        do {
            $b64 = $dict[($dec % 64)] . $b64;
            $dec /= 64;
        } while ($dec >= 1);
        return $b64;
    }

    public static function aesEncrypt($data)
    {
        $key = 'oScGU3fj8m/tDCyvomEhwI91M1FcwvQqWuFpPoDHlFk=';
        $iv = 'w2wJCnctEG09danPPI7SxQ==';
        $encrypted = openssl_encrypt($data, 'aes-256-cbc', base64_decode($key), OPENSSL_RAW_DATA, base64_decode($iv));
        return base64_encode($encrypted);
    }

    public static function aesDncrypt($data)
    {
        $key = 'oScGU3fj8m/tDCyvomEhwI91M1FcwvQqWuFpPoDHlFk=';
        $iv = 'w2wJCnctEG09danPPI7SxQ==';
        $encrypted = base64_decode($data);
        $decrypted = openssl_decrypt($encrypted, 'aes-256-cbc', base64_decode($key), OPENSSL_RAW_DATA, base64_decode($iv));
        return $decrypted;
    }

    public static function aesEncryptone($data)
    {
        $key = 'Cd2sd56Msd789123';
        $iv = 'A123456Msd789123';
        $encrypted = base64_encode(openssl_encrypt($data,"AES-128-CBC",$key,OPENSSL_RAW_DATA,$iv));
        return $encrypted;
    }

    public static function aesDncryptone($data)
    {
        $key = 'Cd2sd56Msd789123';
        $iv = 'A123456Msd789123';
        $decrypted = openssl_decrypt(base64_decode($data), 'AES-128-CBC', $key,OPENSSL_RAW_DATA,$iv);
        return $decrypted;
    }

    public static function md5Encrypt($data)
    {
        $sign = md5("Bo".$data."Komd");
        return $sign;
    }

    /**
     * 获取指定日期段内每一天的日期
     * @param  Date  $startdate 开始日期
     * @param  Date  $enddate   结束日期
     * @return Array
     */
    public static function getDateFromRange($startdate, $enddate,$format){
 
        $stimestamp = strtotime($startdate);
        $etimestamp = strtotime($enddate);
 
        // 计算日期段内有多少天
        $days = ($etimestamp-$stimestamp)/86400;
 
        // 保存每天日期
        $date = array();
 
        for($i=0; $i<$days; $i++){
            $date[] = date($format, $stimestamp+(86400*$i));
        }
        return $date;
    }

    public static function getSHortLinksId($short_id)
    {
        $index = -1;
        $len = strlen($short_id);
        for ($i = 0; $i < $len; $i++) {
            $char = substr($short_id, $i, 1);
            $isCh = preg_match("/^[" . chr(0xa1) . "-" . chr(0xff) . "]+$/", $char); // 判断是否是中文
            if ($isCh) {
                $index = $i;
                break;
            }
        }
        $short_id = substr($short_id,0,$index);
        return $short_id;
    }

    public static function userAgentSystem()
    {
        // 判断访问是什么系统
        $userAgent = $_SERVER['HTTP_USER_AGENT'];
        $system = "Other";
        if (strpos($userAgent, 'Windows NT')) {
            $system = "Windows";
        } elseif(strpos($userAgent, 'iPhone')){
            $system = "iPhone";
        } elseif(strpos($userAgent, 'iPad')){
            $system = "iPad";
        } elseif(strpos($userAgent, 'Android')){
            $system = "Android";
        } elseif(strpos($userAgent, 'Macintosh')){
            $system = "Mac";
        } elseif(strpos($userAgent, 'Linux x86_64')){
            $device = "Linux";
        } else {
            $system = "Other";
        }
        return $system;
    }

    public static function userAgentDevice()
    {
        // 判断访问是什么设备
        $userAgent = $_SERVER['HTTP_USER_AGENT'];
        if (strpos($userAgent, 'HUAWEI') || strpos($userAgent, 'Huawei')) {
            $device = "HUAWEI";
        } elseif(strpos($userAgent, 'vivo') || strpos($userAgent, 'Vivo') || strpos($userAgent, 'V1901A') || strpos($userAgent, 'V2001A') || strpos($userAgent, 'V1831A') || strpos($userAgent, 'V2031A') || strpos($userAgent, 'V1838A') || strpos($userAgent, 'V1911A') || strpos($userAgent, 'V2005A') || strpos($userAgent, 'V1829A') || strpos($userAgent, 'V1821A') || strpos($userAgent, 'V2046A') || strpos($userAgent, 'V2002A') || strpos($userAgent, 'V1832A') || strpos($userAgent, 'V2031EA') || strpos($userAgent, 'V1809A')){
            $device = "Vivo";
        } elseif(strpos($userAgent, 'HONOR')){
            $device = "HONOR";
        } elseif(strpos($userAgent, 'Xiaomi') || strpos($userAgent, 'XiaoMi') || strpos($userAgent, 'MI 6') || strpos($userAgent, 'MI 7') || strpos($userAgent, 'MI 8') || strpos($userAgent, 'MI 9') || strpos($userAgent, 'MI 10') || strpos($userAgent, 'MI 11') || strpos($userAgent, 'MI Note') || strpos($userAgent, 'M2011K2C') || strpos($userAgent, 'M2006C3LC') || strpos($userAgent, 'MIX') || strpos($userAgent, 'M2002J9E')){
            $device = "Xiaomi";
        } elseif(strpos($userAgent, 'Redmi') || strpos($userAgent, 'Redmi') || strpos($userAgent, 'M2104K10AC') || strpos($userAgent, 'M2004J7AC') || strpos($userAgent, 'M2006J10C')){
            $device = "Redmi";
        } elseif(strpos($userAgent, 'OPPO') || strpos($userAgent, 'oppo') || strpos($userAgent, 'PDVM') || strpos($userAgent, 'PDAM')  || strpos($userAgent, 'PDEM') || strpos($userAgent, 'PCPM') || strpos($userAgent, 'PDSM') || strpos($userAgent, 'PEHM') || strpos($userAgent, 'PDYT') || strpos($userAgent, 'PCAM') || strpos($userAgent, 'PCHM') || strpos($userAgent, 'PCCM') || strpos($userAgent, 'PEGM') || strpos($userAgent, 'PBCM') || strpos($userAgent, 'PCLM') || strpos($userAgent, 'PDBM') || strpos($userAgent, 'PDYM') || strpos($userAgent, 'PBEM') || strpos($userAgent, 'PADM') || strpos($userAgent, 'PACM') || strpos($userAgent, 'PCGM') || strpos($userAgent, 'PCAT') || strpos($userAgent, 'PCRM') || strpos($userAgent, 'PDPM') || strpos($userAgent, 'PBFT') || strpos($userAgent, 'PAFM') || strpos($userAgent, 'PCKM') || strpos($userAgent, 'PEHT') || strpos($userAgent, 'PBDM') || strpos($userAgent, 'PBFM') || strpos($userAgent, '00 Build') || strpos($userAgent, 'PCDM10')){
            $device = "OPPO";
        } elseif(strpos($userAgent, 'V2073A') || strpos($userAgent, 'V2055A') || strpos($userAgent, 'V2012A') || strpos($userAgent, 'V1955A') || strpos($userAgent, 'V1986A')){
            $device = "iQOO";
        } elseif(strpos($userAgent, '; RMX')){
            $device = "Realme";
        } elseif(strpos($userAgent, 'DT1902A')){
            $device = "Jianguo";
        } elseif(strpos($userAgent, 'OnePlus') || strpos($userAgent, 'ONEPLUS') || strpos($userAgent, 'KB2000')){
            $device = "OnePlus";
        } elseif(strpos($userAgent, 'samsung') || strpos($userAgent, 'Samsung') || strpos($userAgent, 'SAMSUNG')){
            $device = "Samsung";
        } elseif(strpos($userAgent, 'iPhone')){
            $device = "iPhone";
        } elseif(strpos($userAgent, 'iPad')){
            $device = "iPad";
        } elseif(strpos($userAgent, 'Macintosh')){
            $device = "Mac";
        } elseif(strpos($userAgent, 'MP1602') || strpos($userAgent, 'Meitu')){
            $device = "Meitu";
        } elseif(strpos($userAgent, 'MEIZU') || strpos($userAgent, 'MZ-M5s') || strpos($userAgent, 'M5 Note')){
            $device = "Meizu";
        } elseif(strpos($userAgent, 'Hisense')){
            $device = "Hisense";
        } elseif(strpos($userAgent, 'Linux x86_64')){
            $device = "Linux";
        } elseif(strpos($userAgent, 'QP1A') || strpos($userAgent, 'RP1A') || strpos($userAgent, 'QD4A') || strpos($userAgent, 'QQ3A') || strpos($userAgent, 'QQ2A') || strpos($userAgent, 'QQ1A') || strpos($userAgent, 'D1A') || strpos($userAgent, 'PPR1') || strpos($userAgent, 'PPR2') || strpos($userAgent, 'PQ1A') || strpos($userAgent, 'PQ2A')){
            $device = "Pixel";
        } else {
            $device = "Other";
        }
        return $device;
    }

    public static function userAgentBrowser()
    {
        // 判断访问是什么设备
        $userAgent = $_SERVER['HTTP_USER_AGENT'];
        if (strpos($userAgent, 'MicroMessenger')) {
            // 微信浏览器
            $browser = "MicroMessenger";
        } elseif(strpos($userAgent, 'QQBrowser')){
            // 电脑QQ浏览器
            $browser = "QQBrowser";
        } elseif(strpos($userAgent, 'VivoBrowser')){
            // Vivo浏览器
            $browser = "VivoBrowser";
        } elseif(strpos($userAgent, 'HeyTapBrowser')){
            // Oppo浏览器
            $browser = "HeyTapBrowser";
        } elseif(strpos($userAgent, 'HuaweiBrowser')){
            // 华为浏览器
            $browser = "HuaweiBrowser";
        } elseif(strpos($userAgent, 'MiuiBrowser')){
            // 小米浏览器
            $browser = "MiuiBrowser";
        } elseif(strpos($userAgent, 'MQQBrowser')){
            // QQ浏览器
            $browser = "MQQBrowser";
        } elseif(strpos($userAgent, 'AlipayClient')){
            // 支付宝浏览器
            $browser = "AlipayClient";
        } elseif(strpos($userAgent, 'baiduboxapp')){
            // 百度APP
            $browser = "baiduboxapp";
        } elseif(strpos($userAgent, 'Weibo')){
            // 微博浏览器
            $browser = "Weibo";
        } elseif(strpos($userAgent, 'iqiyi')){
            // 爱奇艺浏览器
            $browser = "iqiyi";
        } elseif(strpos($userAgent, 'aweme')){
            // 小米浏览器
            $browser = "aweme";
        } elseif(strpos($userAgent, 'kdtUnion_iting')){
            // 喜马拉雅
            $browser = "kdtUnion_iting";
        } elseif(strpos($userAgent, 'DingTalk')){
            // 钉钉
            $browser = "DingTalk";
        } elseif(strpos($userAgent, 'UCBrowser')){
            // UC浏览器
            $browser = "UCBrowser";
        } elseif(strpos($userAgent, 'SamsungBrowser')){
            // 三星浏览器
            $browser = "SamsungBrowser";
        } elseif(strpos($userAgent, 'NokiaBrowser')){
            // 诺基亚浏览器
            $browser = "NokiaBrowser";
        } elseif(strpos($userAgent, 'Opera')){
            // 欧朋
            $browser = "Opera";
        } elseif(strpos($userAgent, 'Firefox')){
            // 火狐
            $browser = "Firefox";
        } elseif(strpos($userAgent, 'Zhihu') || strpos($userAgent, 'hihu')){
            // 知乎
            $browser = "Zhihu";
        } elseif(strpos($userAgent, 'LarkLocale')){
            $browser = "Feishu";
        } elseif(strpos($userAgent, 'iPhone')){
            $browser = "iPhone";
        } elseif(strpos($userAgent, 'iPad')){
            $browser = "iPad";
        } elseif(strpos($userAgent, 'Macintosh')){
            $browser = "Mac";
        } else {
            $browser = "Other";
        }
        return $browser;
    }

    public static function getDevice($weight)
    {
        $userAgent = $_SERVER['HTTP_USER_AGENT'];
        if (strpos($userAgent, 'iPhone')) {
            if ($weight == "1284") {
                $modal = "12 ProMax";
            } elseif($weight == "1242"){
                $modal = "11 ProMax/XsMax";
            } elseif($weight == "1170"){
                $modal = "12/12Pro";
            } elseif($weight == "828"){
                $modal = "11/XR";
            } elseif($weight == "1125"){
                $modal = "11 Pro/X/Xs";
            } elseif($weight == "1242"){
                $modal = "6p,6sp,7p,8Plus";
            } elseif($weight == "1080"){
                $modal = "iPhone12 mini";
            } elseif($weight == "750"){
                $modal = "iPhone6,6s,7,8,SE";
            } elseif($weight == "640*1136"){
                $modal = "iPhone5,5s,5c";
             } elseif($weight == "640*960"){
                $modal = "iPhone4,4s";
            } else {
                $modal = "iPhone2G,3G,3GS";
            }
            return $modal;
        } elseif(strpos($userAgent, 'Android')){
            try {
                preg_match('/\((.*)\)/', $userAgent, $match);
                $deviceLists = explode(";",$match[1]);
                $newLists = [];
                foreach ($deviceLists as $deviceList) {
                    $deviceListEmpty = str_replace(" ","",$deviceList);
                    if ($deviceListEmpty != "Linux" && $deviceListEmpty != "U" && $deviceListEmpty != "zh-cn" && !strpos($deviceList, 'Android') && !strpos($deviceList, 'HMSCore') && !strpos($deviceList, 'HarmonyOS')) {
                        $newLists[] = $deviceList;
                    }
                }
                if (count($newLists) == 1) {
                    $Build = explode("Build",$newLists[0]);
                    if (count($Build) != 1) {
                        $modal = $Build[0];
                    } else {
                        $modal = $newLists[0];
                    }
                } else {
                    $modal = "未知";
                }
                return $modal;
            } catch (Exception $e) {
                return "未知";
            }
        } else {
            return "未知";
        }
    }
}
